package org.JWCB.friend.service.question;

import cn.hutool.core.collection.CollectionUtil;
import org.JWCB.common.core.constants.CacheConstants;
import org.JWCB.common.core.constants.QuestionTagFieldName;
import org.JWCB.common.core.enums.QuestionTagEnums;
import org.JWCB.common.redis.service.RedisService;
import org.JWCB.friend.domain.question.entity.QuestionTag;
import org.JWCB.friend.domain.question.vo.QuestionTagVo;
import org.JWCB.friend.mapper.question.QuestionTagMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class QuestionTagService {


    @Autowired
    private RedisService redisService;

    @Autowired
    private QuestionTagMapper questionTagMapper;
    /**
     * 获取题目所有的标签
     * @param questionId
     * @return
     */
    public List<QuestionTagVo> getQuestionAllTag(Long questionId) {
        QuestionTag questionTag = questionTagMapper.selectById(questionId);
        if(questionTag == null) {
            return new ArrayList<>();
        }
        QuestionTagEnums[] values = QuestionTagEnums.values();
        List<QuestionTagVo> questionTagStrList = new ArrayList<>();
        for (QuestionTagEnums value : values) {
            if(containsTag(questionTag,value)) {
                QuestionTagVo questionTagVo = new QuestionTagVo();;
                questionTagVo.setCode(value.getCode());
                questionTagVo.setDesc(value.getDesc());
                questionTagStrList.add(questionTagVo);
            }
        }
        return questionTagStrList;
    }




    /**
     * 从缓存中获取标签信息
     * @param questionId
     * @return
     */
    private List<String> getQuestionTagFromCache(Long questionId) {
        String cacheKey = CacheConstants.QUESTION_TAG_CACHE_LIST_LEY + questionId;
        List<String> questionTagList = redisService.getCacheListByRange(cacheKey, 0, -1, String.class);
        if(CollectionUtil.isEmpty(questionTagList)) {
            return new ArrayList<>();
        }
        return questionTagList;
    }


    /**
     * 获取到所有标签描述
     * @param questionId
     * @return
     */
    private List<String> getQuestionAllTagStr(Long questionId) {
        QuestionTag questionTag = questionTagMapper.selectById(questionId);
        if(questionTag == null) {
            return new ArrayList<>();
        }
        QuestionTagEnums[] values = QuestionTagEnums.values();
        List<String> questionTagStrList = new ArrayList<>();
        for (QuestionTagEnums value : values) {
            if(containsTag(questionTag,value)) {
                questionTagStrList.add(value.getDesc());
            }
        }
        return questionTagStrList;
    }

    private boolean containsTag(QuestionTag questionTag,QuestionTagEnums questionTagEnum) {
        if(questionTagEnum.getFieldName().equals(QuestionTagFieldName.TAG_INFO_01)) {
            return (questionTag.getTagInfo01() & questionTagEnum.getTag()) == questionTagEnum.getTag();
        }else if(questionTagEnum.getFieldName().equals(QuestionTagFieldName.TAG_INFO_02)) {
            return (questionTag.getTagInfo02() & questionTagEnum.getTag()) == questionTagEnum.getTag();
        }else if(questionTagEnum.getFieldName().equals(QuestionTagFieldName.TAG_INFO_03)) {
            return (questionTag.getTagInfo03() & questionTagEnum.getTag()) == questionTagEnum.getTag();
        }else if(questionTagEnum.getFieldName().equals(QuestionTagFieldName.TAG_INFO_04)) {
            return (questionTag.getTagInfo04() & questionTagEnum.getTag()) == questionTagEnum.getTag();
        }
        return false;
    }

    /**
     * 为标签缓存设置缓存空值
     * @param questionId
     */
    private void setNonValue(Long questionId) {
        String cacheKey = CacheConstants.QUESTION_TAG_CACHE_LIST_LEY + questionId;
        redisService.leftPushForList(cacheKey,CacheConstants.QUESTION_TAG_CACHE_NON_VALUE);
        redisService.expire(cacheKey,CacheConstants.QUESTION_TAG_CACHE_NON_VALUE_EXP,TimeUnit.MINUTES);
    }



}
